package admin

import (
	"anban/service"
	"anban/utils"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
)

type CardNumLibController struct {
	BaseController
}

func (c *CardNumLibController) Add() {
	p := map[string]interface{}{}
	p["status"] = 1
	_, c.Data["schoolList"] = service.GetSchoolList(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/card_num_lib/add.html"
}

func (c *CardNumLibController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()

	if len(input["school_id"][0]) <= 0 {
		res = utils.ResJson(0, "请选择学校", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["count"][0]) <= 0 {
		res = utils.ResJson(0, "请输入生成个数", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	_, err := service.AddCardNums(input)
	if err != nil {
		res = utils.ResJson(0, "生成失败:" + err.Error(), "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res = utils.ResJson(1, "生成成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *CardNumLibController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")

	p := map[string]interface{}{}
	schoolName := c.GetString("school_name")
	c.Data["schoolName"] = schoolName
	if len(schoolName) > 0 {
		p["schoolName"] = schoolName
	}
	cardNum := c.GetString("card_num")
	c.Data["cardNum"] = cardNum
	if len(cardNum) > 0 {
		p["cardNum"] = cardNum
	}
	p["curPage"] = curPage
	p["perCount"] = perCount
	p["relation"] = true
	p["convert"] = true
	totalCount, recordList := service.GetCardNumLibList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/card_num_lib/list.html"
}
